/*
 * @(#)DatepickerTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.util.RequestUtils;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.Utility;

/**
TimepickerTag.java
<p>This class is to extend the TextTag provided by Apache Struts.<p>
<p>Modification in "struts-html.tld"<br>
    
  @author          Vera Wang
  @company         DCIVision Limited
  @creation date   10/27/2005
  @version         $Revision: 1.1.26.1 $
*/

public class TimepickerTag extends TextTag{

  /** Holds value of property ontimechange. */
  private String ontimechange = null;  
    
  /** Getter for property ontimechange.
   * @return Value of property ontimechange.
   */
  public String getOntimechange() {
    return this.ontimechange;
  }

  /** Setter for property ontimechange.
   * @param ontimechange New value of property ontimechange.
   */
  public void setOntimechange(String ontimechange) {
    this.ontimechange = ontimechange;
  }
    
  /** overriden parent's doStartTag
   * @return
   * @throws JspException
   */
  public int doStartTag() throws javax.servlet.jsp.JspException {
    int retValue = SKIP_BODY;
    JspWriter out = pageContext.getOut();
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
    String actionMode = (String) request.getAttribute(GlobalConstant.ACTION_MODE);
    String sPropertyName = this.getProperty();
    String sTimeStr = this.getValue();
    String sOnTimeChangeStr = this.getOntimechange();
    try { 
    	if (!actionMode.equals(GlobalConstant.NAV_MODE_VIEW)){
      	int iHourPart=-1, iMinutePart=-1;
        if (value==null){
          sTimeStr = (String) RequestUtils.lookup(pageContext, name, property, null);   
        }       
      	if (!Utility.isEmpty(sTimeStr) && !"null".equals(sTimeStr)) {
          iHourPart = new Integer(sTimeStr.substring(0,2)).intValue();
          iMinutePart = new Integer(sTimeStr.substring(3,5)).intValue();
        }else{
          sTimeStr = ""; 
        }
        sOnTimeChangeStr = " this.form.elements['"+property+"'].value = this.form.elements['hour_" + sPropertyName +"'].value + ':' + this.form.elements['min_" + sPropertyName +"'].value;return;";
        this.setOntimechange(sOnTimeChangeStr);
       
      	//hour selection box
      	out.print("<select name=\"hour_"+this.property+"\" ");
      	out.print("onchange=\""+this.getOntimechange()+"\" ");
      	out.println(">");
      	for (int i=0; i<24; i++) {
      		out.print("  <option value=\""+(i<10? ("0"+i): Integer.toString(i))+"\" "+(iHourPart==i? "selected": "")+">"+(i<10? ("0"+i): Integer.toString(i))+"</option>");
      	}
      	out.println("</select>");
    	
      	out.println(":");
    	
      	//minute selection box
      	out.print("<select name=\"min_"+this.property+"\" ");
      	out.print("onchange=\""+this.getOntimechange()+"\" ");
      	out.println(">");
      	for (int i=0; i<60; i++) {
      		out.print("  <option value=\""+(i<10? ("0"+i): Integer.toString(i))+"\" "+(iMinutePart==i? "selected": "")+">"+(i<10? ("0"+i): Integer.toString(i))+"</option>");
      	}
      	out.println("</select>");
      	out.print("<input type=\"hidden\" name=\"" + this.getProperty() + "\"  value=\"" + sTimeStr + "\" >");
      } else {
        retValue = super.doStartTag();
      }
    	this.setValue(null);
    	return retValue;
    } catch (Exception e) {
      log.error(e, e);
    	throw new JspException(e);
    }
  }
  
}
